package Agency.models;
import Agency.views.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
import java.util.*;




/**
 *
 * @author wangari
 */
public class NewHouseModel  extends javax.swing.JFrame{
    Connection conn = null;
   private  ActionListener actionlistener;
    public AddNewHouse myHouse;
    ArrayList<HouseModel> myList = new ArrayList<>();
    
   /**
    * creating an object of the  new house model
    */ 
    public NewHouseModel(){
       myHouse= new  AddNewHouse();
       //myHouse.setVisible(true);
    }
    /**
     * method to close the view, it first clears all the fields and then closes the view
     */
     public void closeView(){
     myHouse.getRent().setText(null);
     myHouse.getPlotNum().setText(null);
     myHouse.setVisible(false);  
     }
     /**
      * Adding a new house which is vacant, the house details are location, house type
      * house rent  and the house plot number
      */
     public void newHouseController(){
       
        String houseType =myHouse.getHseType().getSelectedItem().toString();
        String houseTownLocation = myHouse.getTownLocation().getSelectedItem().toString();
        double houseRent  = Double.parseDouble(myHouse.getRent().getText());
        String housePlotNum = myHouse.getPlotNum().getText();
        
  try{
       Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection(
 "jdbc:mysql://localhost/MusyiAgency?user=root&password=");
 
 PreparedStatement p=conn.prepareStatement("INSERT INTO  AllHouses(HouseType  ,TownLocation,HouseRent,HousePlotNumber)Values(?,?,?,?)");
            
    p.setString(1, houseType);
    p.setString(2, houseTownLocation);
    p.setDouble(3,houseRent);
    p.setString(4,housePlotNum);
HouseModel ourHouse = new HouseModel(houseType,houseTownLocation,houseRent,housePlotNum);
myList.add(ourHouse);
    p.execute(); 
    JOptionPane.showMessageDialog(null,"House added successfully");
    myHouse.getRent().setText(null);
     myHouse.getPlotNum().setText(null);
    p.close();
   
    }catch(Exception e){
        System.out.println("Error"+e.toString());
              
    }
 }
     
 
     }
    
//}
